//
//  ViewController.m
//  MXTuYaSweeperDemo
//
//  Created by MUXIN-iOS on 2024/6/3.
//

#import "ViewController.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import <ThingSmartHomeKit/ThingSmartKit.h>
#import <ThingSmartSweeperKit/ThingSmartSweeperKit.h>

#define WEAKSELF __weak __typeof(self) weakSelf = self;
#define STRONGSELF __strong __typeof(self) strongSelf = weakSelf;

@interface ViewController ()<ThingSmartHomeManagerDelegate, ThingSmartHomeDelegate, ThingSmartSweeperDeviceDelegate>

@property (nonatomic, strong) ThingSmartHomeManager *homeManager;

@property (nonatomic, strong) ThingSmartHome *home;

@property (nonatomic, strong) ThingSmartSweeperDevice *sweeper;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initP2P];
}

- (void)initP2P {
    // 登录
    WEAKSELF
    [[ThingSmartUser sharedInstance] loginByEmail:@"+86" email:@"pizhenzhen@mxi.ai" password:@"Pp12345678.." success:^{
        NSLog(@"MX_ 登录成功");
        [weakSelf fetchHomeData];
    } failure:^(NSError *error) {
        NSLog(@"MX_ 登录失败:%@", error);
    }];
}

- (void)fetchHomeData {
    WEAKSELF
    [self.homeManager getHomeListWithSuccess:^(NSArray<ThingSmartHomeModel *> *homes) {
        NSLog(@"MX_ 获取家庭列表成功");
        if (homes.count > 0) {
            weakSelf.home = [ThingSmartHome homeWithHomeId:homes.firstObject.homeId];
            weakSelf.home.delegate = weakSelf;
            [weakSelf fetchDeviceList];
        }
    } failure:^(NSError *error) {
        NSLog(@"MX_ 获取家庭列表失败:%@", error);
    }];
}

- (void)fetchDeviceList {
    WEAKSELF
    [weakSelf.home getHomeDataWithSuccess:^(ThingSmartHomeModel *homeModel) {
        NSLog(@"MX_ 获取家庭详情成功");
//        NSArray *deviceList = weakSelf.home.deviceList;
        NSArray *sharedDeviceList = weakSelf.home.sharedDeviceList;
        // 只取被分享的第一个设备
        ThingSmartDeviceModel *deviceModel = sharedDeviceList.firstObject;
        [weakSelf initP2PDevice:deviceModel.devId];
    } failure:^(NSError *error) {
        NSLog(@"MX_ 获取家庭详情失败:%@", error);
    }];
}

- (void)initP2PDevice:(NSString *)devId {
    self.sweeper = [ThingSmartSweeperDevice deviceWithDeviceId:devId];
    self.sweeper.delegate = self;
    NSLog(@"MX_ 初始化P2P成功:%@", devId);
}



#pragma mark - IBAction

- (IBAction)connectP2P:(id)sender {
    [self.sweeper connectDeviceByP2P:YES success:^{
        NSLog(@"MX_ 连接P2P成功 p2p session is active:%d", [self.sweeper P2PActiveCheck]);
    } failure:^(NSError * _Nullable error) {
        NSLog(@"MX_ 连接P2P失败 %@", error);
    }];
}

- (IBAction)downloadP2PData:(id)sender {
    [self.sweeper startObserverSweeperDataByP2P:ThingSmartSweeperP2PDownloadTypeStill receiveP2PData:^(NSDictionary * _Nonnull data) {
        NSLog(@"MX_ 下载P2P数据成功:%@", data);
    } failure:^(NSError * _Nullable error) {
        NSLog(@"MX_ 下载P2P数据失败:%@", error);
    }];
}

- (IBAction)stopDownloadP2PData:(id)sender {
    [self.sweeper stopObserverSweeperDataByP2P:^{
        NSLog(@"MX_ 停止下载P2P数据成功");
    } failure:^(NSError * _Nullable error) {
        NSLog(@"MX_ 停止下载P2P数据失败:%@", error);
    }];
}

- (IBAction)destoryP2P:(id)sender {
    NSLog(@"MX_ %s", __func__);
    NSLog(@"MX_ 开始销毁P2P");
    [self.sweeper onDestroyP2P];
    self.sweeper.delegate = nil;
    self.sweeper = nil;
}

- (void)dealloc
{
    NSLog(@"MX_ %s", __func__);
    NSLog(@"MX_ 开始销毁P2P");
    [self.sweeper onDestroyP2P];
}

#pragma mark - Lazy load
- (ThingSmartHomeManager *)homeManager {
    if (!_homeManager) {
        _homeManager = [[ThingSmartHomeManager alloc] init];
        _homeManager.delegate = self;
    }
    return _homeManager;
}

@end
